//
//  SISSRecommandRentCell.m
//  SISSAPP
//
//  Created by 袁应荣 on 2017/12/18.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSRecommandRentCell.h"

@interface SISSRecommandRentCell ()

/**
 线下图片
 */
@property (weak, nonatomic) UIImageView *offlineImageView;

@end
@implementation SISSRecommandRentCell



- (void)setRentOutModel:(SISSRentOutModel *)rentOutModel {
    [super setRentOutModel:rentOutModel];
    
    self.recommendButton.selected = rentOutModel.isSelected;
}

- (void)setTenantModel:(SISSTenantModel *)tenantModel {
    [super setTenantModel:tenantModel];
    
    self.recommendButton.selected = tenantModel.isSelected;
}

/**
 点击推荐按钮
 */
- (void)checkButtonClickAction:(UIButton *)sender {
    
    if ([self.recommandDelegate respondsToSelector:@selector(recommandRentCell:checkButtonIsSelected:)]) {
        BOOL doSelection = [self.recommandDelegate recommandRentCell:self checkButtonIsSelected:!self.recommendButton.isSelected];
        
        if(!doSelection){
            
        } else {
            if (self.rentOutModel) {
                self.rentOutModel.isSelected = !self.rentOutModel.isSelected;
                self.recommendButton.selected = self.rentOutModel.isSelected;
            } else if (self.tenantModel) {
                self.tenantModel.isSelected = !self.tenantModel.isSelected;
                self.recommendButton.selected = self.tenantModel.isSelected;
            }
        }
    }
}

#pragma mark - UI
- (void)initUI {
    [super initUI];
    
    UIButton *checkButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [checkButton setImage:[UIImage imageNamed:@"check"] forState:UIControlStateNormal];
    [checkButton setImage:[UIImage imageNamed:@"checkSelected"] forState:UIControlStateSelected];
    [checkButton addTarget:self action:@selector(checkButtonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [checkButton setBackgroundImage:[PublicUtil imageWithColor:[UIColor clearColor] size:CGSizeMake(60, 60)] forState:UIControlStateNormal];
    [self.contentView addSubview:checkButton];
    self.recommendButton = checkButton;
    
    UIImageView *offlineImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"offline"]];
    [offlineImageView sizeToFit];
    offlineImageView.hidden = YES;
    [self.contentView addSubview:offlineImageView];
    self.offlineImageView = offlineImageView;
}

#pragma mark - 约束
- (void)initConstraint {
    [super initConstraint];
    
    WeakSelf
    CGFloat margin = 8.0;
    CGSize buttonSize = CGSizeMake(44, 44);
    [self.recommendButton mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(buttonSize);
    }];
    
    [self.offlineImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.contentView).offset(-margin);
        make.top.equalTo(weakSelf.contentView);
    }];
}

@end
